We’ve already thrown around a few new terms, so let’s define them before we proceed.
Unfortunately, this course will not be able to give a comprehensive introduction in R as a whole. However, there are lots of available R resources online to help your training (e.g., Harvard’s Chan Schools Introductory Course or Roger Peng’s Coursera courses).
R also has some of the best library documentation with a standardised requirement of full manuals and vignettes required to list libraries on the main repositories (e.g., CRAN or Bioconducter).
All of you will have learned at least 1 programming language or stastical package so the usual advice applies: experiment, look up the documentation, feel free to google wildly and often!
For each practical you will have to create a new rmarkdown notebook that contains the answers and any code required to reproducibly generate them (in bold)
You will then submit the knitted notebook in html format via
Brightspace and include a link to this notebook in a git repository of
your choice (this could be in a public or private git repo or Dal’s
institutional account, just enable access from my username
github
:fmaguire or gitlab.cs.dal.ca
:finlaym if
it is a private repository).
These are due the midnight before the next week’s practical.
Go to the RStudio website to download and install the software.
You may well have git already installed but if not please install it following the official documentation.
Rstudio is split up into 4 components by default (although this can be tweaked in the settings):
On the bottom left is the Console Terminal (where you can run R code and any GUI-based interactions will be documented with appropriate R code)
On the bottom right is the Files pane (lists files in your project)
On the top left is the Editor pane (standard code editing/highlighting as appropriate with vim/emacs keybindings configurable)
On the top right is the Environment pane (lists all currently defined variables)
Try typing x <- 2
in the Console and hit enter, what
do you get in the Environment pane?
Create a new Rproject and configure git to work with this project (connected to the appropriate repository on gitlab/github).
myVector <- c(1,4,3,2)
myVector
## [1] 1 4 3 2
print(myVector[2]) # print the second element from beginning of vector
## [1] 4
print(myVector[-2]) # print the second element from the end of vector
## [1] 1 3 2
print(myVector[2:3]) # print the element from second to third of the vector
## [1] 4 3
print(myVector[myVector > 2]) # print all element bigger that 2
## [1] 4 3
cat("The second element of x is ", myVector[2], "\n") # print with format
## The second element of x is 4
print(length(myVector)) # print the length of the vector
## [1] 4
myVector <- c(myVector, 90) # adding at the end
myVector <- c(30, myVector) # adding at the beginning
In R, a factor is a categorical variable that represents a discrete number of possible values, each of which belongs to a specific category. Factors are often used to represent qualitative or nominal data. The levels of a factor are the unique values that it can take. by default based on alphabet
expression <- c("low", "high", "medium", "high", "low", "medium", "high")
expression <- factor(expression)
expression
## [1] low high medium high low medium high
## Levels: high low medium
# to change the order
levels(expression) <- c("low", "medium", "high")
expression
## [1] medium low high low medium high low
## Levels: low medium high
# Create a factor with custom levels
education <- c("high school", "bachelor's", "master's", "bachelor's", "high school")
education_factor <- factor(education, levels = c("high school", "bachelor's", "master's"))
print(education_factor)
## [1] high school bachelor's master's bachelor's high school
## Levels: high school bachelor's master's
In R, a matrix is a two-dimensional data structure that contains elements of the same data type, arranged in rows and columns. It is a useful data structure for storing and manipulating numeric data.
m <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2)
print(m)
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
# get the shape of matrix
shape <- dim(m)
print(shape)
## [1] 3 2
# re-shape of the matrix
dim(m) <- c(2, 3)
print(m)
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
# to print the first row
print(m[1, ])
## [1] 1 3 5
# to print the second column
print(m[, 2])
## [1] 3 4
# to create a new matrix of the column 2 to 3
m2 <- cbind(m[, 2:3])
print(m2)
## [,1] [,2]
## [1,] 3 5
## [2,] 4 6
In R, a dataframe is a two-dimensional rectangular table-like structure that consists of rows and columns. The rows correspond to cases (observations), while the columns correspond to variables (attributes). Each column in a dataframe can have a different data type (e.g., character, numeric, factor, etc.), but all columns must have the same length. Dataframes are the most commonly used data structure in R and are often used for data manipulation, cleaning, and analysis.
df <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(25, 30, 35),
gender = c("female", "male", "male")
)
print(df)
## name age gender
## 1 Alice 25 female
## 2 Bob 30 male
## 3 Charlie 35 male
print(df$age) # return an array
## [1] 25 30 35
print(df["age"]) # return an df
## age
## 1 25
## 2 30
## 3 35
x = c(1,2,3)
y = c(2,5,3)
Some statistical functions:
** we can also have a chain of functions **
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
x = c(1,2,3)
y = c(2,5,3)
# to chain together multiple functions in a single line of code
x <- sqrt(16) %>% exp() # pass the output of sqrt to exp input and ...
x
## [1] 54.59815
R packages are collections of functions, data, and documentation that extend the capabilities of R. They are a powerful tool that makes it easy to perform complex tasks in R. Once installed, R packages can be loaded into R sessions using the library() function. It is important to note that even if you have previously installed a package, you will need to load it again in each new R session to be able to use it.
You can also use search() function to check which library are loaded so far
# install packages only once
# install.packages(c("datasauRus", "tidyverse", "dplyr", "readr", "tibble", "ggplot2"))
# load only needed packages in each project
library(datasauRus)
library(tidyverse)
library(dplyr)
library(readr)
library(tibble)
library(ggplot2)
search()
## [1] ".GlobalEnv" "package:lubridate" "package:forcats"
## [4] "package:stringr" "package:purrr" "package:readr"
## [7] "package:tidyr" "package:tibble" "package:ggplot2"
## [10] "package:tidyverse" "package:datasauRus" "package:dplyr"
## [13] "package:stats" "package:graphics" "package:grDevices"
## [16] "package:utils" "package:datasets" "package:methods"
## [19] "Autoloads" "package:base"
The core tidyverse includes the packages that you’re likely to use in everyday data analyses. As of tidyverse 1.3.0, the following packages are included in the core tidyverse: ggplot2, dplyr, tidyr, readr, purrr, tibble, stringr, forcats
The tidyverse also includes many other packages with more specialized usage. They are not loaded automatically with library(tidyverse), so you’ll need to load each one with its own call to library().
dplyr provides a grammar of data manipulation, providing a consistent set of verbs that solve the most common data manipulation challenges.
Used for adding new variables to a dataset. Here’s an example of adding a new variable that calculates the total price of a product based on its unit price and quantity:
Python equivalent: pandas.DataFrame.assign().
# Create sample data
df <- data.frame(product = c("A", "B", "C"),
unit_price = c(10, 20, 30),
quantity = c(5, 2, 3))
# Add new variable for total price
df <- df %>%
mutate(total_price = unit_price * quantity)
# View updated data
df
## product unit_price quantity total_price
## 1 A 10 5 50
## 2 B 20 2 40
## 3 C 30 3 90
Used for selecting specific variables from a dataset. Here’s an example of selecting only the product and quantity variables from the df
Python equivalent: pandas.DataFrame.loc[:, column_names] or pandas.DataFrame[column_names].
df_selected <- df %>%
select(product, quantity)
# Output the resulting dataframe
df_selected
## product quantity
## 1 A 5
## 2 B 2
## 3 C 3
Used for filtering rows based on a condition. Here’s an example of filtering a dataset to only include rows where the unit_price is greater than or equal to 20
Python equivalent: pandas.DataFrame.query().
# filter rows where the unit_price is greater than or equal to 20
filtered_df <- df %>%
filter(unit_price >= 20)
# print the filtered dataframe
print(filtered_df)
## product unit_price quantity total_price
## 1 B 20 2 40
## 2 C 30 3 90
Used for sorting a dataset by one or more variables. Here’s an example of sorting the df by the quantity variable in descending order:
Python equivalent: pandas.DataFrame.sort_values()
# Sort data by cylinders in descending order
df %>%
arrange(desc(quantity))
## product unit_price quantity total_price
## 1 A 10 5 50
## 2 C 30 3 90
## 3 B 20 2 40
Used to summarize data into a single value or a small set of values. It is often used in combination with other functions such as group_by()
Python equivalent: pandas.DataFrame.groupby().agg() or pandas.DataFrame.groupby().apply()
df %>%
summarise(total_sales = sum(unit_price * quantity))
## total_sales
## 1 180
a function for grouping a data frame by one or more variables. In this example, we first use the group_by() function to group the data frame by the ‘product’ column. This creates a new object called grouped_df, which contains the original data frame but with rows grouped by the unique values in the ‘product’ column.
Next, we use the summarize() function to calculate summary statistics for each group. This creates a new data frame called summarized_df, which contains one row for each unique value in the ‘product’ column, along with the mean ‘unit_price’ and ‘quantity’ for each group.
Python equivalent: pandas.DataFrame.groupby().
df2 <- data.frame(product = c("A", "B", "C", "A", "B"),
unit_price = c(10, 20, 30, 15, 25),
quantity = c(5, 2, 3, 6, 1))
# Group the data frame by the 'product' column
grouped_df <- dplyr::group_by(df2, product)
# Calculate the mean 'unit_price' and 'quantity' for each group
summarized_df <- dplyr::summarize(grouped_df, avg_unit_price = mean(unit_price), avg_quantity = mean(quantity))
summarized_df
## # A tibble: 3 × 3
## product avg_unit_price avg_quantity
## <chr> <dbl> <dbl>
## 1 A 12.5 5.5
## 2 B 22.5 1.5
## 3 C 30 3
The goal of tidyr is to help you create tidy data. Tidy data is data where:
Tidy data describes a standard way of storing data that is used wherever possible throughout the tidyverse.
This function converts “wide” data to “long” data by gathering columns into key-value pairs. In this example, the gather function is used to convert the data from wide format to long format, with the country column as the id column, the year column as the key column, and the expectancy column as the value column. The resulting dataframe
Python equivalent: pd.melt()
# create a small life expectancy dataset
life_expectancy <- data.frame(
country = c("Australia", "Canada", "France"),
`2010` = c(82.0, 80.7, 81.8),
`2015` = c(82.4, 81.5, 82.3),
`2020` = c(83.0, 81.9, 83.0)
)
life_expectancy
## country X2010 X2015 X2020
## 1 Australia 82.0 82.4 83.0
## 2 Canada 80.7 81.5 81.9
## 3 France 81.8 82.3 83.0
# use gather to convert the data from wide to long format
life_expectancy_long <- gather(life_expectancy, year, expectancy, -country)
# print the long format data
print(life_expectancy_long)
## country year expectancy
## 1 Australia X2010 82.0
## 2 Canada X2010 80.7
## 3 France X2010 81.8
## 4 Australia X2015 82.4
## 5 Canada X2015 81.5
## 6 France X2015 82.3
## 7 Australia X2020 83.0
## 8 Canada X2020 81.9
## 9 France X2020 83.0
This function is the opposite of gather(). It takes key-value pairs and spreads them out into separate columns.
Python equivalent: pd.pivot()
# use spread to convert the data from long to wide format
life_expectancy_wide <- spread(life_expectancy_long, key = year, value = expectancy)
# print the wide format data
print(life_expectancy_wide)
## country X2010 X2015 X2020
## 1 Australia 82.0 82.4 83.0
## 2 Canada 80.7 81.5 81.9
## 3 France 81.8 82.3 83.0
This function separates one column into multiple columns based on a separator character.
Python equivalent: str.split()
df <- data.frame(x = c("a_1", "b_2"))
tidyr::separate(df, x, c("letter", "number"), sep = "_")
## letter number
## 1 a 1
## 2 b 2
This function replaces missing values with the previous non-missing value.
Python equivalent: df.fillna(method=‘ffill’)
df <- data.frame(x = c(1, NA, NA, 4))
tidyr::fill(df, x)
## x
## 1 1
## 2 1
## 3 1
## 4 4
This function removes rows with missing values.
Python equivalent: df.dropna()
df <- data.frame(x = c(1, 2, NA), y = c(3, NA, 5))
tidyr::drop_na(df)
## x y
## 1 1 3
This function replaces missing values with a specified value.
Python equivalent: df.fillna(value)
df <- data.frame(x = c(1, 2, NA), y = c(3, NA, 5))
tidyr::replace_na(df, list(x = 0, y = 99))
## x y
## 1 1 3
## 2 2 99
## 3 0 5
The goal of readr is to provide a fast and friendly way to read rectangular data from delimited files, such as comma-separated values (CSV) and tab-separated values (TSV).
Tibble is an R package that provides a modern reimagining of the data.frame. It offers a suite of functions that make it easy to create, manipulate, and analyze data in R.
Creates a tibble from input vectors or data frames.
Python equivalent: pandas.DataFrame: Creates a DataFrame from input arrays or lists.
# create a tibble with three columns
library(tibble)
tb <- tibble(a = 1:3, b = c("one", "two", "three"), c = TRUE)
# print the tibble
print(tb)
## # A tibble: 3 × 3
## a b c
## <int> <chr> <lgl>
## 1 1 one TRUE
## 2 2 two TRUE
## 3 3 three TRUE
More functions of tibble:
ggplot2 is a popular package in R used for data visualization.
Initializes a plot and sets its default data and aesthetics.
Equivalent in matplotlib: plt.subplots().
# created a blank canvas for the plot. any visual elements haven't added to it yet.
data(mpg)
p <- ggplot(data = mpg, aes(x = displ, y = hwy))
p
geom_point(): This is used to add points to the plot. Here’s an example:
p + geom_point()
geom_line(): This is used to add lines to the plot. Here’s an example:
p + geom_line()
aes(): This is used to specify the aesthetic mappings for the plot. Here’s an example:
p <- ggplot(data = mpg, aes(x = displ, y = hwy, colour = class)) +
geom_point()
p
geom_bar(): This is used to add bar plots to the plot. Here’s an example:
ggplot(data = mpg, aes(x = class)) + geom_bar()
geom_histogram(): This is used to add histograms to the plot. Here’s an example:
ggplot(data = mpg, aes(x = hwy)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
geom_density(): This is used to add density plots to the plot. Here’s an example:
ggplot(data = mpg, aes(x = hwy)) + geom_density()
facet_wrap(): This is used to create a set of plots with one plot for each level of a categorical variable. Here’s an example:
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + facet_wrap(~class)
theme(): This is used to modify the overall appearance of the plot. Here’s an example:
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_bw()
labs(): This is used to modify the axis labels and plot title. Here’s an example:
ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + labs(x = "Engine displacement (L)", y = "Highway MPG", title = "Fuel efficiency vs engine size")
R is an open-source language, and developers contribute functionality
to R via packages. In this practical we will work with three packages:
datasauRus
which contains the dataset, and
tidyverse
which is a collection of packages for doing data
analysis in a “tidy” way (e.g., dplyr
, readr
,
tibble
, and ggplot2
).
Load these packages by running the following in the Console.
library(tidyverse)
library(datasauRus)
If you haven’t installed these packages yet and R complains, then you can install these packages by running the following command. (Note that R package names are case-sensitive)
install.packages(c("tidyverse", "datasauRus"))
Note that the packages are also loaded with the same commands in your R Markdown document.
Before we introduce the data, let’s warm up with some simple exercises.
The top portion of your R Markdown file (between the three dashed lines) is called YAML. It stands for “YAML Ain’t Markup Language”. It is a human friendly data serialization standard for all programming languages. All you need to know is that this area is called the YAML (we will refer to it as such) and that it contains meta information about your document.
0. Open the R Markdown (Rmd) file in your project, change the author name to your name, and knit the document.
The data frame we will be working with today is called
datasaurus_dozen
and it’s in the datasauRus
package. Actually, this single data frame contains 13 datasets, designed
to show us why data visualisation is important and how summary
statistics alone can be misleading. The different datasets are maked by
the dataset
variable.
To find out more about the dataset, type the following in your
Console: ?datasaurus_dozen
. A question mark before the name
of an object will always bring up its help file. This command must be
ran in the Console.
1. Based on the help file, how many rows and how many columns
does the datasaurus_dozen
file have? What are the variables
included in the data frame? (this can be hardcoded)
Let’s take a look at what these datasets are. To do so we can make a frequency table of the dataset variable:
datasaurus_dozen %>%
count(dataset)
## # A tibble: 13 × 2
## dataset n
## <chr> <int>
## 1 away 142
## 2 bullseye 142
## 3 circle 142
## 4 dino 142
## 5 dots 142
## 6 h_lines 142
## 7 high_lines 142
## 8 slant_down 142
## 9 slant_up 142
## 10 star 142
## 11 v_lines 142
## 12 wide_lines 142
## 13 x_shape 142
The original Datasaurus (dino
) was created by Alberto
Cairo in this
great blog post. The other Dozen were generated using simulated
annealing and the process is described in the paper Same Stats,
Different Graphs: Generating Datasets with Varied Appearance and
Identical Statistics through Simulated Annealing by Justin Matejka
and George Fitzmaurice. In the paper, the authors simulate a variety of
datasets that the same summary statistics to the Datasaurus but have
very different distributions.
2. Plot y
vs. x
for the
dino
dataset. Then, calculate the correlation coefficient
between x
and y
for this dataset.
Below is the code you will need to complete this exercise. Basically, the answer is already given, but you need to include relevant bits in your Rmd document and successfully knit it and view the results.
Start with the datasaurus_dozen
and pipe it into the
filter
function to filter for observations where
dataset == "dino"
. Store the resulting filtered data frame
as a new data frame called dino_data
.
dino_data <- datasaurus_dozen %>%
filter(dataset == "dino")
There is a lot going on here, so let’s slow down and unpack it a bit.
First, the pipe operator: %>%
, takes what comes
before it and sends it as the first argument to what comes after it. So
here, we’re saying filter
the datasaurus_dozen
data frame for observations where dataset == "dino"
.
Second, the assignment operator: <-
, assigns the name
dino_data
to the filtered data frame.
Next, we need to visualize these data. We will use the
ggplot
function for this. Its first argument is the data
you’re visualizing. Next we define the aes
thetic mappings.
In other words, the columns of the data that get mapped to certain
aesthetic features of the plot, e.g. the x
axis will
represent the variable called x
and the y
axis
will represent the variable called y
. Then, we add another
layer to this plot where we define which geom
etric shapes
we want to use to represent each observation in the data. In this case
we want these to be points,m hence geom_point
.
ggplot(data = dino_data, mapping = aes(x = x, y = y)) +
geom_point()
If this seems like a lot, it is. And you will learn about the philosophy of building data visualizations in layer in detail as we go along. For now, follow along with the code that is provided.
For the second part of this exercises, we need to calculate a summary
statistic: the correlation coefficient. Correlation coefficient, often
referred to as \(r\) in statistics,
measures the linear association between two variables. You will see that
some of the pairs of variables we plot do not have a linear relationship
between them. This is exactly why we want to visualize first: visualize
to assess the form of the relationship, and calculate \(r\) only if relevant. In this case,
calculating a correlation coefficient really doesn’t make sense since
the relationship between x
and y
is definitely
not linear – it’s dinosaurial!
But, for illustrative purposes, let’s calculate correlation
coefficient between x
and y
.
Start with dino_data
and calculate a summary statistic
that we will call r
as the cor
relation between
x
and y
.
dino_data %>%
summarize(r = cor(x, y))
3. Plot y
vs. x
for the
star
dataset. You can (and should) reuse code we introduced
above, just replace the dataset name with the desired dataset. Then,
calculate the correlation coefficient between x
and
y
for this dataset. How does this value compare to the
r
of dino
?
4. Plot y
vs. x
for the
circle
dataset. You can (and should) reuse code we
introduced above, just replace the dataset name with the desired
dataset. Then, calculate the correlation coefficient between
x
and y
for this dataset. How does this value
compare to the r
of dino
?
Facet by the dataset variable, placing the plots in a 3 column grid, and don’t add a legend.
5. Finally, let’s plot all datasets at once. In order to do this we will make use of facetting.
ggplot(datasaurus_dozen, aes(x = x, y = y, color = dataset))+
geom_point()+
facet_wrap(~ dataset, ncol = 3) +
theme(legend.position = "none")
And we can use the group_by
function to generate all
correlation coefficients.
datasaurus_dozen %>%
group_by(dataset) %>%
summarize(r = cor(x, y))
You’re done with the data analysis exercises, but we’d like you to do two more things:
Click on the gear icon in on top of the R Markdown document, and select “Output Options…” in the dropdown menu. In the pop up dialogue box go to the Figures tab and change the height and width of the figures, and hit OK when done. Then, knit your document and see how you like the new sizes. Change and knit again and again until you’re happy with the figure sizes. Note that these values get saved in the YAML.
You can also use different figure sizes for different figures. To do
so click on the gear icon within the chunk where you want to make a
change. Changing the figure sizes added new options to these chunks:
fig.width
and fig.height
. You can change them
by defining different values directly in your R Markdown document as
well.
Once again click on the gear icon in on top of the R Markdown document, and select “Output Options…” in the dropdown menu. In the General tab of the pop up dialogue box try out different syntax highlighting and theme options. Hit OK and knit your document to see how it looks. Play around with these until you’re happy with the look.
If you have time you can explore the different ways you can add styling to your rmarkdown document.
Here is a cheatsheet
and a markdown cheatsheet
This set of lab exercises have been adapted from Mine Çetinkaya-Rundel’s class Introduction to Data Science and PM566